package com.estar.assistsurvy.activity;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.entity.RequestLog;
import com.estar.assistsurvy.entity.ResponseRgt;
import com.estar.assistsurvy.entity.TSRole;
import com.estar.assistsurvy.entity.User;
import com.estar.assistsurvy.thread.LoginThread;
import com.estar.assistsurvy.utils.CheckInfoUtils;
import com.estar.assistsurvy.utils.MD5Utils;
import com.estar.assistsurvy.utils.MightypleUtil;
import com.estar.assistsurvy.utils.SharepreferenceUtils;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import cn.jpush.android.api.JPushInterface;

public class LoginActivity extends BaseActivity implements OnClickListener{

	private Intent intent;
	private TextView loginPage_regist,loginPage_forgetPwd;
	private Button loginPage_loginBt;
	private EditText loginPage_userTxt,loginPage_pwdTxt;
	private ProgressDialog dialog;
	private String tel,password,deviceId,deviceToken,UUID;
	private SharedPreferences preferences;
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(dialog!=null){
				dialog.dismiss();
			}
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;
				
			case 0:
				ResponseRgt login=new ResponseRgt();
				login=(ResponseRgt) msg.obj;
				
				if(login.getCode().equals("1")){
					
					User user=login.getUser();
					SharepreferenceUtils.saveUser(user);

					TSRole role=login.getRole();
					if(role!=null&&!role.equals("")){
						SharepreferenceUtils.saveRole(role);
					}
					showShortToast(login.getMessages());
					intent=new Intent(LoginActivity.this,MainActivity.class);
					startActivity(intent);
					finish();
				}else{
					showShortToast(login.getMessages());
				}
				
				break;

			default:
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initView();
	}
	

	private void initView(){

		preferences=getSharedPreferences("user", Context.MODE_PRIVATE);
		UUID=preferences.getString("uuid", "");
		
		deviceId=MightypleUtil.getDeviceId(getApplicationContext());
		deviceToken=JPushInterface.getRegistrationID(getApplicationContext());
		
		loginPage_userTxt=find(R.id.loginPage_userTxt);
		SharedPreferences share=getSharedPreferences("username", Context.MODE_PRIVATE);
		String t=share.getString("telephone", "");
		Log.i("wl", "------>"+t);
//		if(!preferences.getString("phoneNumber", "").equals("")){
			loginPage_userTxt.setText(t);
//		}
		tel=loginPage_userTxt.getText().toString();
		loginPage_pwdTxt=find(R.id.loginPage_pwdTxt);

		loginPage_forgetPwd=find(R.id.loginPage_forgetPwd);
		loginPage_forgetPwd.setOnClickListener(this);

		loginPage_regist=find(R.id.loginPage_regist);
		loginPage_regist.setOnClickListener(this);

		loginPage_loginBt=find(R.id.loginPage_loginBt);
		loginPage_loginBt.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()){
		case R.id.loginPage_regist:
			intent=new Intent(LoginActivity.this,RegistActivity.class);
			startActivity(intent);
			break;

		case R.id.loginPage_forgetPwd:
			intent=new Intent(LoginActivity.this,ForgetPwdActivity.class);
			startActivity(intent);
			break;

		case R.id.loginPage_loginBt:
			
			tel=loginPage_userTxt.getText().toString().trim();
			password=loginPage_pwdTxt.getText().toString().trim();
			if /*(deviceToken.isEmpty()) {
				showShortToast("当前网络信号较差!");
			}else if*/(MightypleUtil.isEmpty(password)){
				showShortToast("请输入密码!");
			}else if(!new CheckInfoUtils().isPhoneNumberValid(tel)){
				showShortToast("手机号码格式不正确!");
			}else{
				RequestLog requestLog=new RequestLog(UUID,deviceId, tel, MD5Utils.getMD5(password), "1", deviceToken);
				new LoginThread(LoginActivity.this, handler, requestLog).start();
				dialog=ProgressDialog.show(this, null, "登录中…", false, false);
			}

			break;

		default:
			break;
		}
	}
	private long exitTime=0;
	@SuppressLint("ShowToast") @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
			if(System.currentTimeMillis()-exitTime>2000){
				showShortToast("再按一次退出");
			exitTime=System.currentTimeMillis();
			}
			else{
				finish();
			System.exit(0);}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
//	private void setDialog(){
//		dialog=new ProgressDialog(LoginActivity.this,ProgressDialog.THEME_HOLO_LIGHT);
//		dialog.setTitle("登录中，请稍后…");
//		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//		dialog.show();
//		new Thread(new Runnable() {
//			int progress=0;
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				while(progress<100){
//					dialog.setProgress(progress);
//					try {
//						Thread.sleep(200);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					progress++;
//				}
//			}
//		}).start();
//	}

}
